package com.loozb.thread2;

import java.util.ArrayList;

/**
 *
 * 测试线程安全
 * @author 龙召碧
 * @create 2018-01-30 16:07
 **/
public class ArrayListMultiThread {
    static ArrayList<Integer> al = new ArrayList<Integer>(10);
    public static synchronized void add(int i) {
        al.add(i);
    }

    public static class AddThread implements Runnable {
        public void run() {
            for(int i = 0; i < 1000000; i++) {
                add(i);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new AddThread());
        Thread t2 = new Thread(new AddThread());
        t1.start();t2.start();
        t1.join();t2.join();
        System.out.println(al.size());
    }
}
